import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    addlist: [],
    userlist: []
  },
  mutations: {
    ADDLIST(state, item) {
      state.addlist = item
    },
    GOTOUSER(state, item) {
      var isShow = false
      state.userlist.map((ele) => {
        if (ele.id == item.id) {
          ele.count++
          isShow = true;
          return
        }
      })
      if(!isShow){
        item.count=1;
        item.checked=false;
        state.userlist.push(item);
      }
      // state.userlist.push(item);
      localStorage.userlist = JSON.stringify(state.userlist)
    }
  },
  actions: {
    ADDLIST(context) {
      axios.get('http://api.kudesoft.cn/tdk/goods').then((res) => {
        context.commit('ADDLIST', res.data.data.data.list)
        console.log(res.data.data.data.list);
      }).catch((err) => [
        console.log(err)
      ])
    },
    GOTOUSER(context, item) {
      context.commit('GOTOUSER', item)
    }
  },
  modules: {
  }
})
